<?php
/**
 * 文章中心
 * power by huanran.zhao
 * QQ: 523994553
 * 2013-04-09
 */
class ListingApp extends CommonApp {
	
	private $table = 'datas';

	/**
	 * 首页
	 */
	public function indexAct(){
		$item = My::arr2var($_REQUEST,'item');
		if(!My::is_item($item))
			$this->ajaxReturn(404,'文章分类有误!');
		//search
		$search = array();
		$search['where'] = "1 ";
		$search['where'] .= "AND A.`item`='{$item}' ";
		//院系的全部信息
		$first_children = M('category')->item2submenu('class',array('level'=>'1'));
		$this->assign('first_children',$first_children);
		//publish
		if(My::arr2var($_GET,'publish')==='0'){
			$search['where'] .= "AND A.publish=0 ";
		}elseif(My::arr2var($_GET,'publish')==1){
			$search['where'] .= "AND A.publish=1 ";
		}
		$search['field'] = "A.*";
		$search['page'] = My::arr2var($_REQUEST,'page').(isset($_SESSION['pagesize'])?$_SESSION['pagesize']:'');//用于参与缓存标识
		$search['table'] = "#__{$this->table} as A";
		$search['join']  = "LEFT JOIN #__{$this->table}_params as B ON A.id=B.idid ";
		//搜索产品分类
		foreach($_GET as $key=>$value){
			if(substr($key,0,7)=='params.'){
				$_k = substr($key,7);
				$search['join'] .= "INNER JOIN #__{$this->table}_params as {$_k} ON {$_k}.`value`='{$value}' AND {$_k}.`key`='{$_k}' AND A.id={$_k}.idid ";
			}
		}
		//搜索标题
		if(My::arr2var($_GET,'title')!=''){
			$title = I('get.title');
			$search['where'] .= "AND A.`title` like '%{$title}%' ";
		}
		//搜索标题
		if(My::is_id(My::arr2var($_GET,'id'))){
			$id = $_GET['id'];
			$search['where'] .= "AND A.`id`='{$id}' ";
		}

		//搜索院系
		if(My::arr2var($_GET,'college')!='') {
			$search['where'] .= "AND `college` = ".I('get.college')." ";
			//select当前的专业
			$children = M('category')->id2submenu($_GET['college'],array('level'=>'1'));
			$this->assign('children',$children);
			//搜索专业
			if(My::arr2var($_GET,'major')!='') {
				$search['where'] .= "AND `major` = ".I('get.major')." ";

			}
		}

		$result=My::getCache($search,array('pre'=>$this->table));
		if($result==''){
			//page
			$total_sql = DB('')->table($search['table'])->field('1')->join($search['join'])->where($search['where'])->group('A.id')->select(false);
			$result['page'] = My::page(My::sql2count($total_sql));
			//result
			$result['data'] = DB('')->table($search['table'])->field($search['field'])->join($search['join'])->where($search['where'])->group('A.id')->order('A.ordering ASC,A.adddate DESC,A.id DESC')->limit($result['page']['limits'])->select();
			//echo DB('')->getLastSql();
			if(is_array($result['data']))
			foreach($result['data'] as &$value){
				$value['params'] = My::decodeJson($value['params']);
				$value['college'] = My::id2field('category',$value['college'],'title');
				$value['major'] = My::id2field('category',$value['major'],'title');
			}
			My::setCache($search,$result,array('pre'=>$this->table));
		}
		$this->assign('result',$result);
		//print_r($result);
		//display
		$tpl = I('get.tpl',$item);
		$this->display("Listing/{$tpl}/index");
	}
	
	/**
	 * 页面显示(编辑/添加)
	 * @params $_GET['id']
	 */
	public function modifyAct(){
		//分类唯一名称
		$item = My::arr2var($_GET,'item');
		if(!My::is_item($item))
			$this->ajaxReturn(404,"分类错误!");
		//id
		$id = I('get.id',0);
		$result = array();
		//添加用户界面显示
		$first_children = M('category')->item2submenu('class',array('level'=>'1'));
		//院系的一维数组
		$this->assign('first_children',$first_children);
		if(My::is_id($id)){
			//当前id文章数据
			$result = DB($this->table)->where("id={$id}")->find();
			$result['content'] = My::decodeHtml($result['content']);
			$result['params'] = My::decodeJson($result['params']);
			//保留原始的分类数值
			$result = My::id2detail('datas',$id);
			//找到当前二级的id所对应的parent_id
			$children_id = My::id2field('category',$result['major'],'parent_id');
			$children = M('category')->id2submenu($children_id,array('level'=>'1'));
			$this->assign('result',$result);
			//当前的所有二级分类
			$this->assign('children',$children);
			$this->assign('result',$result);
		}
		//分类id->自定义表单
		//$FORM = Form::item2FORM($item);
		//$this->assign('FORM',$FORM);
		$this->assign('item',$item);
		$tpl = I('get.tpl',$item);
		$this->display("Listing/{$tpl}/modify");
	}
	
	/**
	 * 保存(编辑/添加)
	 * @param string $_POST['URL'] 当前页面url
	 * @param string $_POST['URL_'] 上一页面url
	 * @param int $_POST['id']  当前操作id(可选)
	 * @param $_POST post表单数据
	 */
	public function saveAct(){
		$item = My::arr2var($_POST,'item');
		if(!My::is_item($item))
			$this->ajaxReturn(300,'item有误!');
		$this->verify($item);
		if(!token())
			$this->ajaxReturn(300,'令牌验证错误!');
		if(My::arr2var($_POST,'username')=='')
			$this->ajaxReturn(300,"姓名不能为空!");
		if(My::arr2var($_POST,'number')=='')
			$this->ajaxReturn(300,"学号不能为空!");
		if(My::arr2var($_POST,'college')=='')
			$this->ajaxReturn(300,"请选择专业!");
		if(My::arr2var($_POST,'major')=='')
			$this->ajaxReturn(300,"请选择班级!");
		if(My::arr2var($_POST,'grad_date')=='')
			$this->ajaxReturn(300,"请填写毕业时间!");
		if(strtotime(My::arr2var($_POST,'grad_date'))>strtotime(date("Y-m-d H:i:s")))
			$this->ajaxReturn(300,"毕业时间不能大于当前时间!");
		$id = $_POST['id'] = My::arr2var($_POST,'id');
		$params = My::arr2var($_POST,'params');
		if(My::is_id($id)){//编辑
			/*//白名单
			if($item=='activity')
				$params = My::keepParams($params,array('table'=>$this->table,'id'=>$id,'verifyId'=>'activity'));*/
			//保存表单模型参数
			//My::setParams($this->table,$params,$id,$item);
			//dump($_POST);exit;
			$_POST['editdate'] = date("Y-m-d H:i:s");
			$ok = DB($this->table)->clear()->save($_POST);
			if($ok)	$this->ajaxReturn(200,"编辑成功");
			else		$this->ajaxReturn(300,"编辑失败");
		}else{//添加
			$_POST['adddate'] = date("Y-m-d H:i:s");
			$_POST['author'] = My::user('username');
			$ok = DB($this->table)->clear()->add($_POST);
			$data['username'] = $_POST['username'];
			$data['number'] = $_POST['number'];
			$data['sex'] = $_POST['sex'];
			$data['password'] = md5(md5($_POST['number']));
			$ok = DB('members')->clear()->add($data);
			//保存表单模型参数
			//My::setParams($this->table,$params,$ok,$item);
			if($ok>=0)	$this->ajaxReturn(200,"添加成功");
			else		$this->ajaxReturn(300,"添加失败");
		}
	}

	/**
	 * 当前院系下的专业联动
	 */
	public function collegeid2majorAct(){
		$parent_id = $_POST['college'];
		//判断是否选择一级分类
		if (!My::is_id(My::arr2var($_POST,'college')))
			$this->ajaxReturn(300,'<option>--班级--</option>');
		//找出一级对应的二级id和title的一维数组;
		$children = M('category')->id2submenu($parent_id,array('level'=>'1'));
		//$data在$children,为空时，能传一个空的值返回前台。
		$data ='';
		//判断是是否有该一维数组;
		if(is_array($children)) {
			foreach ($children as $value => $key) {
				$data .= "<option value=".$key['id'].">".$key['title']."</option>";
			}
		}	
		//$data为空时，还能返回数据会前台。
		$this->ajaxReturn(200,$data);
	}

	//就业信息
	public function infoAct(){
		//分类唯一名称
		$item = My::arr2var($_GET,'item');
		if(!My::is_item($item))
			$this->ajaxReturn(404,"分类错误!");
		//id
		$number = I('get.number',0);
		$result = array();

		$type = C('type');
        $related = C('related');
        $parent_id = DB('members')->field('id')->where("number={$number}")->find();
        $result = DB('info')->where("parent_id='".$parent_id['id']."'")->select();
        foreach ($result as &$value) {
            //获取市区
            $value['province'] = M('Address')->getProvince($value['province']);
            //获取城市
            $value['city'] = M('Address')->getCity($value['city']);
            //获取地区
            if(isset($value['area']))
                $value['area'] = M('Address')->getArea($value['area']);
            $value['type'] = $type[$value['type']];
            $value['related'] = $related[$value['related']];
        }
        $this->assign('result',$result);
		//分类id->自定义表单
		//$FORM = Form::item2FORM($item);
		//$this->assign('FORM',$FORM);
		$this->assign('item',$item);
		$tpl = I('get.tpl',$item);
		$this->display("Listing/{$tpl}/info");
	}

	//个人信息
	public function owninfoAct(){
		//分类唯一名称
		$item = My::arr2var($_GET,'item');
		if(!My::is_item($item))
			$this->ajaxReturn(404,"分类错误!");
		//id
		$number = I('get.number',0);
		$result = array();

		$type = C('type');
        $related = C('related');
        $result = DB('members')->where("number={$number}")->find();
        $result['major'] = My::id2field('category',$result['major'],'title');
        $result['year'] = My::id2field('category',$result['year'],'title');
		//分类id->自定义表单
		//$FORM = Form::item2FORM($item);
		//$this->assign('FORM',$FORM);
		$this->assign('result',$result);
		$this->assign('item',$item);
		$tpl = I('get.tpl',$item);
		$this->display("Listing/{$tpl}/owninfo");
	}

}